/**
 * Baijiahulian.com Inc.
 * Copyright (c) 2014-2016 All Rights Reserved.
 */
package com.baijia.util;

import com.baijia.dao.po.AuthToken;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.util.SortedMap;

/**
 * @title TokenUtils
 * @desc TODO 
 * @author macrohuang
 * @date Jan 22, 2016
 * @version 1.0
 */
public class TokenUtils {
    public static boolean validateSign(AuthToken token, String sign, SortedMap<String, Object> params) throws UnsupportedEncodingException{
        if (token==null || StringUtils.isBlank(sign) || CollectionUtils.isEmpty(params)){
            return false;
        }
        StringBuilder sb = new StringBuilder();
        for (Object value:params.values()){
            sb.append(""+value);
        }
        sb.append(token.getSecureKey());
        return StringUtils.equals(sign, DigestUtils.md5DigestAsHex(sb.toString().getBytes("utf8")));
    }
}
